import { Injectable } from "@nestjs/common";
import OpenAI from "openai";
import { HttpService } from "@nestjs/axios";
import { lastValueFrom } from "rxjs";

@Injectable()
export class ExportService {
  constructor(private readonly httpService: HttpService) {

  }

  // async askAi(content: string) {
  //   const openai = new OpenAI({
  //     apiKey: "6788fbf5-37c6-46b3-961d-eab5c6fcb99e",
  //     baseURL: "https://ark.cn-beijing.volces.com/api/v3",
  //     dangerouslyAllowBrowser: true
  //   });
  //   const completion = await openai.chat.completions.create({
  //     messages: [{ role: "user", content: content }],
  //     model: "ep-20241118094221-f2nmw"
  //   });
  //   return completion.choices[0].message.content;
  // }
  async askAi(content:string){
    const headers = {
      'Authorization': `Bearer app-OK0wCn5S7Vnz3AoXPev93iiY`, // 根据API要求调整认证方式
      'Content-Type': 'application/json',
    };
    const data = {
      query: content,
      user:"8910",
      response_mode:"blocking",
      inputs:{}
    }

    try {
      const response = await lastValueFrom(
        this.httpService.post('https://ai.nbsedu.com.cn:49443/v1/chat-messages', data, { headers })
      );
      return response.data.answer;
    } catch (error) {
      throw new Error(`API请求失败: ${error.message}`);
    }
  }
}
